package com.coffee.excel.builder;

import com.coffee.excel.MochaExcelConstant;
import com.coffee.excel.MochaImportHandler;
import com.coffee.excel.executor.ImportExcelExecutor;
import com.coffee.excel.metadata.MochaExcelField;
import com.coffee.excel.metadata.MochaImportParameter;
import org.apache.poi.ss.usermodel.IndexedColors;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

/**
 * @author: songkui
 * @since: 2023/2/14 11:01
 */
public class ImportExcelBuilder extends BasicExcelBuilder {

    private final MochaImportParameter importParameter;

    public ImportExcelBuilder(List<MochaExcelField> fieldList, String title) {
        fieldList.add(new MochaExcelField(-1, IndexedColors.ROSE.getIndex(), false, "mochaImportResult", MochaExcelConstant.IMPORT_RESULT_TITLE, null, null));
        setColumnIndex(fieldList);
        importParameter = new MochaImportParameter();
        importParameter.setFieldList(fieldList);
        importParameter.setTitleName(title);

        importParameter.setHead(headShow(fieldList));
        importParameter.setHeadColorMap(headColorMap(fieldList));
    }

    public ImportExcelBuilder outputStream(OutputStream outputStream) {
        importParameter.setOutputStream(outputStream);
        return this;
    }

    public ImportExcelBuilder inputStream(InputStream inputStream){
        importParameter.setInputStream(inputStream);
        return this;
    }

    public ImportExcelBuilder importBytes(byte[] bytes){
        importParameter.setImportBytes(bytes);
        return this;
    }



    public ImportExcelBuilder importHandler(MochaImportHandler mochaImportHandler){
        importParameter.setMochaImportHandler(mochaImportHandler);
        return this;
    }




    public void doImportTemplate() {
        ImportExcelExecutor importExcelExecutor = new ImportExcelExecutor(importParameter);
        importExcelExecutor.doImportTemplate();
    }

    public void doImport() throws Exception {
        ImportExcelExecutor importExcelExecutor = new ImportExcelExecutor(importParameter);
        importExcelExecutor.doImport();
    }

}
